home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Environments / PowerLisp 2.01 / Supplemental Documentation / Documentation / Chapter 06. Predicates < prev    next >
Text File  |  1995-03-27  |  37KB  |  884 lines

  1. Common Lisp the Language, 2nd Edition
  2. -------------------------------------------------------------------------------
  3.  
  4. 6. Predicates
  5.  
  6. A predicate is a function that tests for some condition involving its arguments
  7. and returns nil if the condition is false, or some non-nil value if the
  8. condition is true. One may think of a predicate as producing a Boolean value,
  9. where nil stands for false and anything else stands for true. Conditional
  10. control structures such as cond, if, when, and unless test such Boolean values.
  11. We say that a predicate is true when it returns a non-nil value, and is false
  12. when it returns nil; that is, it is true or false according to whether the
  13. condition being tested is true or false.
  14.  
  15. By convention, the names of predicates usually end in the letter p (which
  16. stands for ``predicate''). Common Lisp uses a uniform convention in hyphenating
  17. names of predicates. If the name of the predicate is formed by adding a p to an
  18. existing name, such as the name of a data type, a hyphen is placed before the
  19. final p if and only if there is a hyphen in the existing name. For example,
  20. number begets numberp but standard-char begets standard-char-p. On the other
  21. hand, if the name of a predicate is formed by adding a prefixing qualifier to
  22. the front of an existing predicate name, the two names are joined with a hyphen
  23. and the presence or absence of a hyphen before the final p is not changed. For
  24. example, the predicate string-lessp has no hyphen before the p because it is
  25. the string version of lessp (a MacLisp function that has been renamed < in
  26. Common Lisp). The name string-less-p would incorrectly imply that it is a
  27. predicate that tests for a kind of object called a string-less, and the name
  28. stringlessp would connote a predicate that tests whether something has no
  29. strings (is ``stringless'')!
  30.  
  31. The control structures that test Boolean values only test for whether or not
  32. the value is nil, which is considered to be false. Any other value is
  33. considered to be true. Often a predicate will return nil if it ``fails'' and
  34. some useful value if it ``succeeds''; such a function can be used not only as a
  35. test but also for the useful value provided in case of success. An example is
  36. member.
  37.  
  38. If no better non-nil value is available for the purpose of indicating success,
  39. by convention the symbol t is used as the ``standard'' true value.
  40.  
  41. -------------------------------------------------------------------------------
  42.  
  43.    *  Logical Values
  44.    *  Data Type Predicates
  45.         o  General Type Predicates
  46.         o  Specific Data Type Predicates
  47.    *  Equality Predicates
  48.    *  Logical Operators
  49.  
  50. -------------------------------------------------------------------------------
  51.  
  52. 6.1. Logical Values
  53.  
  54. The names nil and t are constants in Common Lisp. Although they are symbols
  55. like any other symbols, and appear to be treated as variables when evaluated,
  56. it is not permitted to modify their values. See defconstant.
  57.  
  58. [Constant]
  59. nil
  60.  
  61. The value of nil is always nil. This object represents the logical false value
  62. and also the empty list. It can also be written ().
  63.  
  64. [Constant]
  65. t
  66.  
  67. The value of t is always t.
  68.  
  69. -------------------------------------------------------------------------------
  70.  
  71. 6.2. Data Type Predicates
  72.  
  73. Perhaps the most important predicates in Lisp are those that deal with data
  74. types; that is, given a data object one can determine whether or not it belongs
  75. to a given type, or one can compare two type specifiers.
  76.  
  77. -------------------------------------------------------------------------------
  78.  
  79.    *  General Type Predicates
  80.    *  Specific Data Type Predicates
  81.  
  82. -------------------------------------------------------------------------------
  83.  
  84. 6.2.1. General Type Predicates
  85.  
  86. If a data type is viewed as the set of all objects belonging to the type, then
  87. the typep function is a set membership test, while subtypep is a subset test.
  88.  
  89. [Function]
  90. typep object type
  91.  
  92. typep is a predicate that is true if object is of type type, and is false
  93. otherwise. Note that an object can be ``of'' more than one type, since one type
  94. can include another. The type may be any of the type specifiers mentioned in
  95. chapter 4 except that it may not be or contain a type specifier list whose
  96. first element is function or values. A specifier of the form (satisfies fn) is
  97. handled simply by applying the function fn to object (see funcall); the object
  98. is considered to be of the specified type if the result is not nil.
  99.  
  100. [change_begin]
  101. X3J13 voted in January 1989 (ARRAY-TYPE-ELEMENT-TYPE-SEMANTICS)   to change
  102. typep to give specialized array and complex type specifiers the same meaning
  103. for purposes of type discrimination as they have for declaration purposes. Of
  104. course, this also applies to such type specifiers as vector and simple-array
  105. (see section 4.5). Thus
  106.  
  107. (typep foo '(array bignum))
  108.  
  109. in the first edition asked the question, Is foo an array specialized to hold
  110. bignums? but under the new interpretation asks the question, Could the array
  111. foo have resulted from giving bignum as the :element-type argument to
  112. make-array?
  113. [change_end]
  114.  
  115. [Function]
  116. subtypep type1 type2
  117.  
  118. The arguments must be type specifiers that are acceptable to typep. The two
  119. type specifiers are compared; this predicate is true if type1 is definitely a
  120. (not necessarily proper) subtype of type2. If the result is nil, however, then
  121. type1 may or may not be a subtype of type2 (sometimes it is impossible to tell,
  122. especially when satisfies type specifiers are involved). A second returned
  123. value indicates the certainty of the result; if it is true, then the first
  124. value is an accurate indication of the subtype relationship. Thus there are
  125. three possible result combinations:
  126.  
  127.  
  128. t    t    type1 is definitely a subtype of type2
  129. nil  t    type1 is definitely not a subtype of type2
  130. nil  nil  subtypep could not determine the relationship
  131.  
  132. [change_begin]
  133. X3J13 voted in January 1989 (SUBTYPEP-TOO-VAGUE)   to place certain
  134. requirements upon the implementation of subtypep, for it noted that
  135. implementations in many cases simply ``give up'' and return the two values nil
  136. and nil when in fact it would have been possible to determine the relationship
  137. between the given types. The requirements are as follows, where it is
  138. understood that a type specifier s involves a type specifier u if either s
  139. contains an occurrence of u directly or s contains a type specifier w defined
  140. by deftype whose expansion involves u.
  141.  
  142.    *  subtypep is not permitted to return a second value of nil unless one or
  143.      both of its arguments involves satisfies, and, or, not, or member.
  144.  
  145.    *  subtypep should signal an error when one or both of its arguments
  146.      involves values or the list form of the function type specifier.
  147.  
  148.    *  subtypep must always return the two values t and t in the case where its
  149.      arguments, after expansion of specifiers defined by deftype, are equal.
  150.  
  151. In addition, X3J13 voted to clarify that in some cases the relationships
  152. between types as reflected by subtypep may be implementation-specific. For
  153. example, in an implementation supporting only one type of floating-point
  154. number, (subtypep 'float 'long-float) would return t and t, since the two types
  155. would be identical.
  156.  
  157. Note that satisfies is an exception because relationships between types
  158. involving satisfies are undecidable in general, but (as X3J13 noted) and, or,
  159. not, and member are merely very messy to deal with. In all likelihood these
  160. will not be addressed unless and until someone is willing to write a careful
  161. specification that covers all the cases for the processing of these type
  162. specifiers by subtypep. The requirements stated above were easy to state and
  163. probably suffice for most cases of interest.
  164.  
  165. X3J13 voted in January 1989 (ARRAY-TYPE-ELEMENT-TYPE-SEMANTICS)   to change
  166. subtypep to give specialized array and complex type specifiers the same meaning
  167. for purposes of type discrimination as they have for declaration purposes. Of
  168. course, this also applies to such type specifiers as vector and simple-array
  169. (see section 4.5).
  170.  
  171. If A and B are type specifiers (other than *, which technically is not a type
  172. specifier anyway), then (array A) and (array B) represent the same type in a
  173. given implementation if and only if they denote arrays of the same specialized
  174. representation in that implementation; otherwise they are disjoint. To put it
  175. another way, they represent the same type if and only if
  176. (upgraded-array-element-type 'A) and (upgraded-array-element-type 'B) are the
  177. same type. Therefore
  178.  
  179. (subtypep '(array A) '(array B))
  180.  
  181. is true if and only if (upgraded-array-element-type 'A) is the same type as
  182. (upgraded-array-element-type 'B).
  183.  
  184. The complex type specifier is treated in a similar but subtly different manner.
  185. If A and B are two type specifiers (but not *, which technically is not a type
  186. specifier anyway), then (complex A) and (complex B) represent the same type in
  187. a given implementation if and only if they refer to complex numbers of the same
  188. specialized representation in that implementation; otherwise they are disjoint.
  189. Note, however, that there is no function called make-complex that allows one to
  190. specify a particular element type (then to be upgraded); instead, one must
  191. describe specialized complex numbers in terms of the actual types of the parts
  192. from which they were constructed. There is no number of type (or rather,
  193. representation) float as such; there are only numbers of type single-float,
  194. numbers of type double-float, and so on. Therefore we want (complex
  195. single-float) to be a subtype of (complex float).
  196.  
  197. The rule, then, is that (complex A) and (complex B) represent the same type
  198. (and otherwise are disjoint) in a given implementation if and only if either
  199. the type A is a subtype of B, or (upgraded-complex-part-type 'A) and
  200. (upgraded-complex-part-type 'B) are the same type. In the latter case (complex
  201. A) and (complex B) in fact refer to the same specialized representation.
  202. Therefore
  203.  
  204. (subtypep '(complex A) '(complex B))
  205.  
  206. is true if and only if the results of (upgraded-complex-part-type 'A) and
  207. (upgraded-complex-part-type 'B) are the same type.
  208.  
  209. Under this interpretation
  210.  
  211. (subtypep '(complex single-float) '(complex float))
  212.  
  213. must be true in all implementations; but
  214.  
  215. (subtypep '(array single-float) '(array float))
  216.  
  217. is true only in implementations that do not have a specialized array
  218. representation for single-float elements distinct from that for float elements
  219. in general.
  220. [change_end]
  221.  
  222. -------------------------------------------------------------------------------
  223.  
  224. 6.2.2. Specific Data Type Predicates
  225.  
  226. The following predicates test for individual data types.
  227.  
  228. [Function]
  229. null object
  230.  
  231. null is true if its argument is (), and otherwise is false. This is the same
  232. operation performed by the function not; however, not is normally used to
  233. invert a Boolean value, whereas null is normally used to test for an empty
  234. list. The programmer can therefore express intent by the choice of function
  235. name.
  236.  
  237. (null x) == (typep x 'null) == (eq x '())
  238.  
  239. [Function]
  240. symbolp object
  241.  
  242. symbolp is true if its argument is a symbol, and otherwise is false.
  243.  
  244. (symbolp x) == (typep x 'symbol)
  245.  
  246. -------------------------------------------------------------------------------
  247. Compatibility note: The Interlisp equivalent of symbolp is called litatom.
  248. -------------------------------------------------------------------------------
  249.  
  250. [Function]
  251. atom object
  252.  
  253. The predicate atom is true if its argument is not a cons, and otherwise is
  254. false. Note that (atom '()) is true, because ()  nil.
  255.  
  256. (atom x) == (typep x 'atom) == (not (typep x 'cons))
  257.  
  258. -------------------------------------------------------------------------------
  259. Compatibility note: In some Lisp dialects, notably Interlisp, only symbols and
  260. numbers are considered to be atoms; arrays and strings are considered to be
  261. neither atoms nor lists (conses).
  262. -------------------------------------------------------------------------------
  263.  
  264. [Function]
  265. consp object
  266.  
  267. The predicate consp is true if its argument is a cons, and otherwise is false.
  268. Note that the empty list is not a cons, so (consp '()) == (consp 'nil) => nil.
  269.  
  270. (consp x) == (typep x 'cons) == (not (typep x 'atom))
  271.  
  272. -------------------------------------------------------------------------------
  273. Compatibility note: Some Lisp implementations call this function pairp or
  274. listp. The name pairp was rejected for Common Lisp because it emphasizes too
  275. strongly the dotted-pair notion rather than the usual usage of conses in lists.
  276. On the other hand, listp too strongly implies that the cons is in fact part of
  277. a list, which after all it might not be; moreover, () is a list, though not a
  278. cons. The name consp seems to be the appropriate compromise.
  279. -------------------------------------------------------------------------------
  280.  
  281. [Function]
  282. listp object
  283.  
  284. listp is true if its argument is a cons or the empty list (), and otherwise is
  285. false. It does not check for whether the list is a ``true list'' (one
  286. terminated by nil) or a ``dotted list'' (one terminated by a non-null atom).
  287.  
  288. (listp x) == (typep x 'list) == (typep x '(or cons null))
  289.  
  290. [Function]
  291. numberp object
  292.  
  293. numberp is true if its argument is any kind of number, and otherwise is false.
  294.  
  295. (numberp x) == (typep x 'number)
  296.  
  297. [Function]
  298. integerp object
  299.  
  300. integerp is true if its argument is an integer, and otherwise is false.
  301.  
  302. (integerp x) == (typep x 'integer)
  303.  
  304. -------------------------------------------------------------------------------
  305. Compatibility note: In MacLisp this is called fixp. Users have been confused as
  306. to whether this meant integerp or fixnump, and so the name integerp has been
  307. adopted here.
  308. -------------------------------------------------------------------------------
  309.  
  310. [Function]
  311. rationalp object
  312.  
  313. rationalp is true if its argument is a rational number (a ratio or an integer),
  314. and otherwise is false.
  315.  
  316. (rationalp x) == (typep x 'rational)
  317.  
  318. [Function]
  319. floatp object
  320.  
  321. floatp is true if its argument is a floating-point number, and otherwise is
  322. false.
  323.  
  324. (floatp x) == (typep x 'float)
  325.  
  326. [change_begin]
  327.  
  328. [Function]
  329. realp object
  330.  
  331. X3J13 voted in March 1989 (REAL-NUMBER-TYPE)   to add the function realp. realp
  332. is true if its argument is a real number, and otherwise is false.
  333.  
  334. (realp x) == (typep x 'real)
  335.  
  336. [change_end]
  337.  
  338. [Function]
  339. complexp object
  340.  
  341. complexp is true if its argument is a complex number, and otherwise is false.
  342.  
  343. (complexp x) == (typep x 'complex)
  344.  
  345. [Function]
  346. characterp object
  347.  
  348. characterp is true if its argument is a character, and otherwise is false.
  349.  
  350. (characterp x) == (typep x 'character)
  351.  
  352. [Function]
  353. stringp object
  354.  
  355. stringp is true if its argument is a string, and otherwise is false.
  356.  
  357. (stringp x) == (typep x 'string)
  358.  
  359. [Function]
  360. bit-vector-p object
  361.  
  362. bit-vector-p is true if its argument is a bit-vector, and otherwise is false.
  363.  
  364. (bit-vector-p x) == (typep x 'bit-vector)
  365.  
  366. [Function]
  367. vectorp object
  368.  
  369. vectorp is true if its argument is a vector, and otherwise is false.
  370.  
  371. (vectorp x) == (typep x 'vector)
  372.  
  373. [Function]
  374. simple-vector-p object
  375.  
  376. vectorp is true if its argument is a simple general vector, and otherwise is
  377. false.
  378.  
  379. (simple-vector-p x) == (typep x 'simple-vector)
  380.  
  381. [Function]
  382. simple-string-p object
  383.  
  384. simple-string-p is true if its argument is a simple string, and otherwise is
  385. false.
  386.  
  387. (simple-string-p x) == (typep x 'simple-string)
  388.  
  389. [Function]
  390. simple-bit-vector-p object
  391.  
  392. simple-bit-vector-p is true if its argument is a simple bit-vector, and
  393. otherwise is false.
  394.  
  395. (simple-bit-vector-p x) == (typep x 'simple-bit-vector)
  396.  
  397. [Function]
  398. arrayp object
  399.  
  400. arrayp is true if its argument is an array, and otherwise is false.
  401.  
  402. (arrayp x) == (typep x 'array)
  403.  
  404. [Function]
  405. packagep object
  406.  
  407. packagep is true if its argument is a package, and otherwise is false.
  408.  
  409. (packagep x) == (typep x 'package)
  410.  
  411. [Function]
  412. functionp object
  413.  
  414. [old_change_begin]
  415. functionp is true if its argument is suitable for applying to arguments, using
  416. for example the funcall or apply function. Otherwise functionp is false.
  417.  
  418. functionp is always true of symbols, lists whose car is the symbol lambda, any
  419. value returned by the function special form, and any values returned by the
  420. function compile when the first argument is nil.
  421. [old_change_end]
  422.  
  423. [change_begin]
  424. X3J13 voted in June 1988 (FUNCTION-TYPE)   to define
  425.  
  426. (functionp x) == (typep x 'function)
  427.  
  428. Because the vote also specifies that types cons and symbol are disjoint from
  429. the type function, this is an incompatible change; now functionp is in fact
  430. always false of symbols and lists.
  431. [change_end]
  432.  
  433. [Function]
  434. compiled-function-p object
  435.  
  436. compiled-function-p is true if its argument is any compiled code object, and
  437. otherwise is false.
  438.  
  439. (compiled-function-p x) == (typep x 'compiled-function)
  440.  
  441. [old_change_begin]
  442. [Function]
  443. commonp object
  444.  
  445. commonp is true if its argument is any standard Common Lisp data type, and
  446. otherwise is false.
  447.  
  448. (commonp x) == (typep x 'common)
  449.  
  450. [old_change_end]
  451.  
  452. [change_begin]
  453. X3J13 voted in March 1989 (COMMON-TYPE)   to remove the predicate commonp (and
  454. the type common) from the language.
  455. [change_end]
  456.  
  457. See also standard-char-p, string-char-p, streamp, random-state-p, readtablep,
  458. hash-table-p, and pathnamep.
  459.  
  460. -------------------------------------------------------------------------------
  461.  
  462. 6.3. Equality Predicates
  463.  
  464. Common Lisp provides a spectrum of predicates for testing for equality of two
  465. objects: eq (the most specific), eql, equal, and equalp (the most general). eq
  466. and equal have the meanings traditional in Lisp. eql was added because it is
  467. frequently needed, and equalp was added primarily in order to have a version of
  468. equal that would ignore type differences when comparing numbers and case
  469. differences when comparing characters. If two objects satisfy any one of these
  470. equality predicates, then they also satisfy all those that are more general.
  471.  
  472. [Function]
  473. eq x y
  474.  
  475. (eq x y) is true if and only if x and y are the same identical object.
  476. (Implementationally, x and y are usually eq if and only if they address the
  477. same identical memory location.)
  478.  
  479. It should be noted that things that print the same are not necessarily eq to
  480. each other. Symbols with the same print name usually are eq to each other
  481. because of the use of the intern function. However, numbers with the same value
  482. need not be eq, and two similar lists are usually not eq. For example:
  483.  
  484. (eq 'a 'b) is false.
  485. (eq 'a 'a) is true.
  486. (eq 3 3) might be true or false, depending on the implementation.
  487. (eq 3 3.0) is false.
  488. (eq 3.0 3.0) might be true or false, depending on the implementation.
  489. (eq #c(3 -4) #c(3 -4))
  490.   might be true or false, depending on the implementation.
  491. (eq #c(3 -4.0) #c(3 -4)) is false.
  492. (eq (cons 'a 'b) (cons 'a 'c)) is false.
  493. (eq (cons 'a 'b) (cons 'a 'b)) is false.
  494. (eq '(a . b) '(a . b)) might be true or false.
  495. (progn (setq x (cons 'a 'b)) (eq x x)) is true.
  496. (progn (setq x '(a . b)) (eq x x)) is true.
  497. (eq #\A #\A) might be true or false, depending on the implementation.
  498. (eq "Foo" "Foo") might be true or false.
  499. (eq "Foo" (copy-seq "Foo")) is false.
  500. (eq "FOO" "foo") is false.
  501.  
  502. In Common Lisp, unlike some other Lisp dialects, the implementation is
  503. permitted to make ``copies'' of characters and numbers at any time. (This
  504. permission is granted because it allows tremendous performance improvements in
  505. many common situations.) The net effect is that Common Lisp makes no guarantee
  506. that eq will be true even when both its arguments are ``the same thing'' if
  507. that thing is a character or number. For example:
  508.  
  509. (let ((x 5)) (eq x x)) might be true or false.
  510.  
  511. The predicate eql is the same as eq, except that if the arguments are
  512. characters or numbers of the same type then their values are compared. Thus eql
  513. tells whether two objects are conceptually the same, whereas eq tells whether
  514. two objects are implementationally identical. It is for this reason that eql,
  515. not eq, is the default comparison predicate for the sequence functions defined
  516. in chapter 14.
  517.  
  518. -------------------------------------------------------------------------------
  519. Implementation note: eq simply compares the two given pointers, so any kind of
  520. object that is represented in an ``immediate'' fashion will indeed have
  521. like-valued instances satisfy eq. In some implementations, for example, fixnums
  522. and characters happen to ``work.'' However, no program should depend on this,
  523. as other implementations of Common Lisp might not use an immediate
  524. representation for these data types.
  525. -------------------------------------------------------------------------------
  526.  
  527. [old_change_begin]
  528. An additional problem with eq is that the implementation is permitted to
  529. ``collapse'' constants (or portions thereof) appearing in code to be compiled
  530. if they are equal. An object is considered to be a constant in code to be
  531. compiled if it is a self-evaluating form or is contained in a quote form. This
  532. is why (eq "Foo" "Foo") might be true or false; in interpreted code it would
  533. normally be false, because reading in the form (eq "Foo" "Foo") would construct
  534. distinct strings for the two arguments to eq, but the compiler might choose to
  535. use the same identical string or two distinct copies as the two arguments in
  536. the call to eq. Similarly, (eq '(a . b) '(a . b)) might be true or false,
  537. depending on whether the constant conses appearing in the quote forms were
  538. collapsed by the compiler. However, (eq (cons 'a 'b) (cons 'a 'b)) is always
  539. false, because every distinct call to the cons function necessarily produces a
  540. new and distinct cons.
  541. [old_change_end]
  542.  
  543. [change_begin]
  544. X3J13 voted in March 1989 (QUOTE-SEMANTICS)   to clarify that eval and compile
  545. are not permitted either to copy or to coalesce (``collapse'') constants (see
  546. eq) appearing in the code they process; the resulting program behavior must
  547. refer to objects that are eql to the corresponding objects in the source code.
  548. Only the compile-file/load process is permitted to copy or coalesce constants
  549. (see section 25.1).
  550. [change_end]
  551.  
  552. [Function]
  553. eql x y
  554.  
  555. The eql predicate is true if its arguments are eq, or if they are numbers of
  556. the same type with the same value, or if they are character objects that
  557. represent the same character. For example:
  558.  
  559. (eql 'a 'b) is false.
  560. (eql 'a 'a) is true.
  561. (eql 3 3) is true.
  562. (eql 3 3.0) is false.
  563. (eql 3.0 3.0) is true.
  564. (eql #c(3 -4) #c(3 -4)) is true.
  565. (eql #c(3 -4.0) #c(3 -4)) is false.
  566. (eql (cons 'a 'b) (cons 'a 'c)) is false.
  567. (eql (cons 'a 'b) (cons 'a 'b)) is false.
  568. (eql '(a . b) '(a . b)) might be true or false.
  569. (progn (setq x (cons 'a 'b)) (eql x x)) is true.
  570. (progn (setq x '(a . b)) (eql x x)) is true.
  571. (eql #\A #\A) is true.
  572. (eql "Foo" "Foo") might be true or false.
  573. (eql "Foo" (copy-seq "Foo")) is false.
  574. (eql "FOO" "foo") is false.
  575.  
  576. Normally (eql 1.0s0 1.0d0) would be false, under the assumption that 1.0s0 and
  577. 1.0d0 are of distinct data types. However, implementations that do not provide
  578. four distinct floating-point formats are permitted to ``collapse'' the four
  579. formats into some smaller number of them; in such an implementation (eql 1.0s0
  580. 1.0d0) might be true. The predicate = will compare the values of two numbers
  581. even if the numbers are of different types.
  582.  
  583. If an implementation supports positive and negative zeros as distinct values
  584. (as in the IEEE proposed standard floating-point format), then (eql 0.0 -0.0)
  585. will be false. Otherwise, when the syntax -0.0 is read it will be interpreted
  586. as the value 0.0, and so (eql 0.0 -0.0) will be true. The predicate = differs
  587. from eql in that (= 0.0 -0.0) will always be true, because = compares the
  588. mathematical values of its operands, whereas eql compares the representational
  589. values, so to speak.
  590.  
  591. Two complex numbers are considered to be eql if their real parts are eql and
  592. their imaginary parts are eql. For example, (eql #C(4 5) #C(4 5)) is true and
  593. (eql #C(4 5) #C(4.0 5.0)) is false. Note that while (eql #C(5.0 0.0) 5.0) is
  594. false, (eql #C(5 0) 5) is true. In the case of (eql #C(5.0 0.0) 5.0) the two
  595. arguments are of different types and so cannot satisfy eql; that's all there is
  596. to it. In the case of (eql #C(5 0) 5), however, #C(5 0) is not a complex number
  597. but is always automatically reduced by the rule of complex canonicalization to
  598. the integer 5, just as the apparent ratio 20/4 is always simplified to 5.
  599.  
  600. The case of (eql "Foo" "Foo") is discussed above in the description of eq.
  601. While eql compares the values of numbers and characters, it does not compare
  602. the contents of strings. To compare the characters of two strings, one should
  603. use equal, equalp, string=, or string-equal.
  604.  
  605. -------------------------------------------------------------------------------
  606. Compatibility note: The Common Lisp function eql is similar to the Interlisp
  607. function eqp. However, eql considers 3 and 3.0 to be different, whereas eqp
  608. considers them to be the same; eqp behaves like the Common Lisp = function, not
  609. like eql, when both arguments are numbers.
  610. -------------------------------------------------------------------------------
  611.  
  612. [Function]
  613. equal x y
  614.  
  615. The equal predicate is true if its arguments are structurally similar
  616. (isomorphic) objects. A rough rule of thumb is that two objects are equal if
  617. and only if their printed representations are the same.
  618.  
  619. Numbers and characters are compared as for eql. Symbols are compared as for eq.
  620. This method of comparing symbols can violate the rule of thumb for equal and
  621. printed representations, but only in the infrequently occurring case of two
  622. distinct symbols with the same print name.
  623.  
  624. Certain objects that have components are equal if they are of the same type and
  625. corresponding components are equal. This test is implemented in a recursive
  626. manner and may fail to terminate for circular structures.
  627.  
  628. For conses, equal is defined recursively as the two car's being equal and the
  629. two cdr's being equal.
  630.  
  631. Two arrays are equal only if they are eq, with one exception: strings and
  632. bit-vectors are compared element-by-element. If either argument has a fill
  633. pointer, the fill pointer limits the number of elements examined by equal.
  634. Uppercase and lowercase letters in strings are considered by equal to be
  635. distinct. (In contrast, equalp ignores case distinctions in strings.)
  636.  
  637. -------------------------------------------------------------------------------
  638. Compatibility note: In Lisp Machine Lisp, equal ignores the difference between
  639. uppercase and lowercase letters in strings. This violates the rule of thumb
  640. about printed representations, however, which is very useful, especially to
  641. novices. It is also inconsistent with the treatment of single characters, which
  642. in Lisp Machine Lisp are represented as fixnums.
  643. -------------------------------------------------------------------------------
  644.  
  645. Two pathname objects are equal if and only if all the corresponding components
  646. (host, device, and so on) are equivalent. (Whether or not uppercase and
  647. lowercase letters are considered equivalent in strings appearing in components
  648. depends on the file name conventions of the file system.) Pathnames that are
  649. equal should be functionally equivalent.
  650.  
  651. [change_begin]
  652. X3J13 voted in June 1989 (EQUAL-STRUCTURE)   to clarify that equal never
  653. recursively descends any structure or data type other than the ones explicitly
  654. described above: conses, bit-vectors, strings, and pathnames. Numbers and
  655. characters are compared as if by eql, and all other data objects are compared
  656. as if by eq.
  657. [change_end]
  658.  
  659. (equal 'a 'b) is false.
  660. (equal 'a 'a) is true.
  661. (equal 3 3) is true.
  662. (equal 3 3.0) is false.
  663. (equal 3.0 3.0) is true.
  664. (equal #c(3 -4) #c(3 -4)) is true.
  665. (equal #c(3 -4.0) #c(3 -4)) is false.
  666. (equal (cons 'a 'b) (cons 'a 'c)) is false.
  667. (equal (cons 'a 'b) (cons 'a 'b)) is true.
  668. (equal '(a . b) '(a . b)) is true.
  669. (progn (setq x (cons 'a 'b)) (equal x x)) is true.
  670. (progn (setq x '(a . b)) (equal x x)) is true.
  671. (equal #\A #\A) is true.
  672. (equal "Foo" "Foo") is true.
  673. (equal "Foo" (copy-seq "Foo")) is true.
  674. (equal "FOO" "foo") is false.
  675.  
  676. To compare a tree of conses using eql (or any other desired predicate) on the
  677. leaves, use tree-equal.
  678.  
  679. [Function]
  680. equalp x y
  681.  
  682. Two objects are equalp if they are equal; if they are characters and satisfy
  683. char-equal, which ignores alphabetic case and certain other attributes of
  684. characters; if they are numbers and have the same numerical value, even if they
  685. are of different types; or if they have components that are all equalp.
  686.  
  687. Objects that have components are equalp if they are of the same type and
  688. corresponding components are equalp. This test is implemented in a recursive
  689. manner and may fail to terminate for circular structures. For conses, equalp is
  690. defined recursively as the two car's being equalp and the two cdr's being
  691. equalp.
  692.  
  693. Two arrays are equalp if and only if they have the same number of dimensions,
  694. the dimensions match, and the corresponding components are equalp. The
  695. specializations need not match; for example, a string and a general array that
  696. happens to contain the same characters will be equalp (though definitely not
  697. equal). If either argument has a fill pointer, the fill pointer limits the
  698. number of elements examined by equalp. Because equalp performs
  699. element-by-element comparisons of strings and ignores the alphabetic case of
  700. characters, case distinctions are therefore also ignored when equalp compares
  701. strings.
  702.  
  703. Two symbols can be equalp only if they are eq, that is, the same identical
  704. object.
  705.  
  706. [change_begin]
  707. X3J13 voted in June 1989 (EQUAL-STRUCTURE)   to specify that equalp compares
  708. components of hash tables (see below), and to clarify that otherwise equalp
  709. never recursively descends any structure or data type other than the ones
  710. explicitly described above: conses, arrays (including bit-vectors and strings),
  711. and pathnames. Numbers are compared for numerical equality (see =), characters
  712. are compared as if by char-equal, and all other data objects are compared as if
  713. by eq.
  714.  
  715. Two hash tables are considered the same by equalp if and only if they satisfy a
  716. four-part test:
  717.  
  718.    *  They must be of the same kind; that is, equivalent :test arguments were
  719.      given to make-hash-table when the two hash tables were created.
  720.  
  721.    *  They must have the same number of entries (see hash-table-count).
  722.  
  723.    *  For every entry (key1, value1) in one hash table there must be a
  724.      corresponding entry (key2, value2) in the other, such that key1 and key2
  725.      are considered to be the same by the :test function associated with the
  726.      hash tables.
  727.  
  728.    *  For every entry (key1, value1) in one hash table and its corresponding
  729.      entry (key2, value2) in the other, such that key1 and key2 are the same,
  730.      equalp must be true of value1 and value2.
  731.  
  732. The four parts of this test are carried out in the order shown, and if some
  733. part of the test fails, equalp returns nil and the other parts of the test are
  734. not attempted.
  735.  
  736. If equalp must compare two structures and the defstruct definition for one used
  737. the :type option and the other did not, then equalp returns nil.
  738.  
  739. If equalp must compare two structures and neither defstruct definition used the
  740. :type option, then equalp returns t if and only if the structures have the same
  741. type (that is, the same defstruct name) and the values of all corresponding
  742. slots (slots having the same name) are equalp.
  743.  
  744. As part of the X3J13 discussion of this issue the following observations were
  745. made. Object equality is not a concept for which there is a uniquely determined
  746. correct algorithm. The appropriateness of an equality predicate can be judged
  747. only in the context of the needs of some particular program. Although these
  748. functions take any type of argument and their names sound very generic, equal
  749. and equalp are not appropriate for every application. Any decision to use or
  750. not use them should be determined by what they are documented to do rather than
  751. by any abstract characterization of their function. If neither equal nor equalp
  752. is found to be appropriate in a particular situation, programmers are
  753. encouraged to create another operator that is appropriate rather than blame
  754. equal or equalp for ``doing the wrong thing.''
  755.  
  756. Note that one consequence of the vote to change the rules of floating-point
  757. contagion (CONTAGION-ON-NUMERICAL-COMPARISONS)   (described in section 12.1) is
  758. to make equalp a true equivalence relation on numbers.
  759. [change_end]
  760.  
  761. (equalp 'a 'b) is false.
  762. (equalp 'a 'a) is true.
  763. (equalp 3 3) is true.
  764. (equalp 3 3.0) is true.
  765. (equalp 3.0 3.0) is true.
  766. (equalp #c(3 -4) #c(3 -4)) is true.
  767. (equalp #c(3 -4.0) #c(3 -4)) is true.
  768. (equalp (cons 'a 'b) (cons 'a 'c)) is false.
  769. (equalp (cons 'a 'b) (cons 'a 'b)) is true.
  770. (equalp '(a . b) '(a . b)) is true.
  771. (progn (setq x (cons 'a 'b)) (equalp x x)) is true.
  772. (progn (setq x '(a . b)) (equalp x x)) is true.
  773. (equalp #\A #\A) is true.
  774. (equalp "Foo" "Foo") is true.
  775. (equalp "Foo" (copy-seq "Foo")) is true.
  776. (equalp "FOO" "foo") is true.
  777.  
  778. -------------------------------------------------------------------------------
  779.  
  780. 6.4. Logical Operators
  781.  
  782. Common Lisp provides three operators on Boolean values: and, or, and not. Of
  783. these, and and or are also control structures because their arguments are
  784. evaluated conditionally. The function not necessarily examines its single
  785. argument, and so is a simple function.
  786.  
  787. [Function]
  788. not x
  789.  
  790. not returns t if x is nil, and otherwise returns nil. It therefore inverts its
  791. argument considered as a Boolean value.
  792.  
  793. null is the same as not; both functions are included for the sake of clarity.
  794. As a matter of style, it is customary to use null to check whether something is
  795. the empty list and to use not to invert the sense of a logical value.
  796.  
  797. [Macro]
  798. and {form}*
  799.  
  800. (and form1 form2 ... ) evaluates each form, one at a time, from left to right.
  801. If any form evaluates to nil, the value nil is immediately returned without
  802. evaluating the remaining forms. If every form but the last evaluates to a
  803. non-nil value, and returns whatever the last form returns. Therefore in general
  804. and can be used both for logical operations, where nil stands for false and
  805. non-nil values stand for true, and as a conditional expression. An example
  806. follows.
  807.  
  808. (if (and (>= n 0)
  809.          (< n (length a-simple-vector))
  810.          (eq (elt a-simple-vector n) 'foo))
  811.     (princ "Foo!"))
  812.  
  813. The above expression prints Foo! if element n of a-simple-vector is the symbol
  814. foo, provided also that n is indeed a valid index for a-simple-vector. Because
  815. and guarantees left-to-right testing of its parts, elt is not called if n is
  816. out of range.
  817.  
  818. To put it another way, the and special form does short-circuit Boolean
  819. evaluation, like the and then operator in Ada and what in some Pascal-like
  820. languages is called cand (for ``conditional and''); the Lisp and special form
  821. is unlike the Pascal or Ada and operator, which always evaluates both
  822. arguments.
  823.  
  824. In the previous example writing
  825.  
  826. (and (>= n 0)
  827.      (< n (length a-simple-vector))
  828.      (eq (elt a-simple-vector n) 'foo)
  829.      (princ "Foo!"))
  830.  
  831. would accomplish the same thing. The difference is purely stylistic. Some
  832. programmers never use expressions containing side effects within and,
  833. preferring to use if or when for that purpose.
  834.  
  835. From the general definition, one can deduce that (and x) == x. Also, (and)
  836. evaluates to t, which is an identity for this operation.
  837.  
  838. One can define and in terms of cond in this way:
  839.  
  840. (and x y z ... w) == (cond ((not x) nil)
  841.                            ((not y) nil)
  842.                            ((not z) nil)
  843.                            ...
  844.                            (t w))
  845.  
  846. See if and when, which are sometimes stylistically more appropriate than and
  847. for conditional purposes. If it is necessary to test whether a predicate is
  848. true of all elements of a list or vector (element 0 and element 1 and element 2
  849. and ...), then the function every may be useful.
  850.  
  851. [Macro]
  852. or {form}*
  853.  
  854. (or form1 form2 ... ) evaluates each form, one at a time, from left to right.
  855. If any form other than the last evaluates to something other than nil, or
  856. immediately returns that non-nil value without evaluating the remaining forms.
  857. If every form but the last evaluates to nil, or returns whatever evaluation of
  858. the last of the forms returns. Therefore in general or can be used both for
  859. logical operations, where nil stands for false and non-nil values stand for
  860. true, and as a conditional expression.
  861.  
  862. To put it another way, the or special form does short-circuit Boolean
  863. evaluation, like the or else operator in Ada and what in some Pascal-like
  864. languages is called cor (for ``conditional or''); the Lisp or special form is
  865. unlike the Pascal or Ada or operator, which always evaluates both arguments.
  866.  
  867. From the general definition, one can deduce that (or x) == x. Also, (or)
  868. evaluates to nil, which is the identity for this operation.
  869.  
  870. One can define or in terms of cond in this way:
  871.  
  872. (or x y z ... w) == (cond (x) (y) (z) ... (t w))
  873.  
  874. See if and unless, which are sometimes stylistically more appropriate than or
  875. for conditional purposes. If it is necessary to test whether a predicate is
  876. true of one or more elements of a list or vector (element 0 or element 1 or
  877. element 2 or ...), then the function some may be useful.
  878.  
  879. -------------------------------------------------------------------------------
  880.  
  881.  
  882.  
  883.  
  884.